<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/1
 * Time: 17:56
 */

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use yii\db\query;
use yii\filters\Cors;
use yii\helpers\ArrayHelper;
use yii\web\models;
use \common\helps\tools;//使用工具类
use yii\db\ActiveRecord;
use yii\db\Connection;
use app\models\Upload;
use yii\web\UploadedFile;

/**
 * Research controller
 */
class ResearchController extends Controller
{
    public function init()
    {
        $this->enableCsrfValidation = false;
    }




    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }



    /**
     * title 立项首页列表
     * Displays homepage.
     *
     * @return string
     */
    public function actionList()
    {
        return $this->render('list');
    }

    /**
     * title 立项详情
     * Displays homepage.
     *
     * @return string
     */
//    public function actionDetailproject()
//    {
//        return $this->render('detailproject');
//    }

    /**
     * title 添加专利
     * Displays homepage.
     *
     * @return string
     */
    public function actionAddpatent()
    {
        return $this->render('addpatent');
    }

    /**
     * title 添加论文
     * Displays homepage.
     *
     * @return string
     */
    public function actionAddthesis()
    {
        return $this->render('addthesis');
    }

    /**
     * title 添加示范应用
     * Displays homepage.
     *
     * @return string
     */
    public function actionAdddemonstration()
    {
        return $this->render('adddemonstration');
    }


    /**
     * title 论文首页
     * Displays homepage.
     *
     * @return string
     */
    public function actionThesislist()
    {
        return $this->render('thesislist');
    }

    /**
     * title 专利首页
     * Displays homepage.
     *
     * @return string
     */
    public function actionPatentlist()
    {
        return $this->render('patentlist');
    }

    /**
     * title 示范应用首页
     * Displays homepage.
     *
     * @return string
     */
    public function actionDemonstrationlist()
    {
        return $this->render('demonstrationlist');
    }

    /**
     * title 成果首页
     * Displays homepage.
     *
     * @return string
     */
    public function actionAchievement()
    {
        return $this->render('achievement');
    }

    /**
     * title 专家管理首页
     * Displays homepage.
     *
     * @return string
     */
    public function actionSpecialist()
    {
        return $this->render('specialist');
    }

    /**
     * title 添加专家管理
     * Displays homepage.
     *
     * @return string
     */
    public function actionAddspecial()
    {
        return $this->render('addspecial');
    }

    /**
     * title 在研详情
     * Displays homepage.
     *
     * @return string
     */
    public function actionDetailthesis()
    {
        return $this->render('detailthesis');
    }

    /**
     * title 专利详情
     * Displays homepage.
     *
     * @return string
     */
    public function actionDetailpatent()
    {
        return $this->render('detailpatent');
    }

    /**
     * title 示范应用详情
     * Displays homepage.
     *
     * @return string
     */
    public function actionDetaildemonstration()
    {
        return $this->render('detaildemonstration');
    }



    /**
     * title 专家管理详情
     * Displays homepage.
     *
     * @return string
     */
    public function actionDetailspecial()
    {
        return $this->render('detailspecial');
    }





    /**
     * 接口
     * title 查询项目名称
     * @return string
     */
    public function actionProjectname()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $research = new \app\models\SpecialOther;
        $list = $research->selectName($uid);
        if($list){
            return tools::ajax_return('0','成功',$list);
        }else{
            return tools::ajax_return('0','成功',[]);
        }

    }


    /**
     * 接口
     * title 在研列表
     * @return string
     */
    public function actionResearchlist()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $research = new \app\models\Research;
        $list = $research->ResearchList($uid);

        if($list){
            return tools::ajax_return('0','成功',$list);
        }else{
            return tools::ajax_return('0','成功',[]);
        }


    }


    /**
     * title 在研详情
     * @return string
     */
    public function actionDetailresearch()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $query =  new \yii\db\Query();
        $id = $post['id'];
        $research = new \app\models\Research;
        $detail = $research->detail($id);
        if(!$detail){
            return tools::ajax_return('0','失败',[]);
        }
        //多家单位
        if($detail['pid'] != '0'){
            $special_project_child = $query->select(['id','project_id','total_money','project_name'])->from('data_yii.special_project_child')->where(['pid' =>$detail['id'] ])->one();
            $detail['child_name'] = $detail['project_name'];
            $detail['project_name'] = $special_project_child['project_name'];

        }else{
            $detail['child_name'] = '';
        }

        //发放方式
        $grank_way = $query->select(['id','content','create_time','update_time'])->from('data_yii.grank_way')->where(['id'=>$detail['grant_way']])->one();
        if(!empty($grank_way['content'])) {
            $detail['way'] = $grank_way['content'];
        }else{
            $detail['way'] = '';
        }

        //论文
        $typeNum = new \app\models\TypeNum;
        $selectNum = $typeNum->selectNum($id);
        if(!empty($selectNum)) {
            foreach($selectNum as $k => $v) {
                $detail['num'][$k]['type_count'] = $v['type_count'];
                $detail['num'][$k]['type'] = $v['type'];
                $detail['num'][$k]['type_id'] = $v['type_id'];
            }
        }else{
            $detail['num'] = [];
        }



        return tools::ajax_return('0','成功',$detail);


    }


    /**
     * title 论文列表
     * @return string
     */
    public function actionThesislistport()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $research = new \app\models\Thesis;
        $list = $research->thesisList($uid);

        return tools::ajax_return('0','成功',$list);



    }


    /**
     * title 添加论文
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionAddportthesis()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        //添加论文
        $create_time = date('Y-m-d H:i:s',time());
        $other = new \app\models\Thesis;
        $result = $other->addThesis($create_time,$uid);

        if($result == 1){
            return tools::ajax_return_error('1','添加失败',[]);
        }


        return tools::ajax_return('0','添加成功',$result);



    }



    /**
     * title 保存论文
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionSaveportthesis()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $thesis_id = $post['thesis_id'];//论文id
        $project_id = $post['project_id'];//项目id
        $project_name = $post['project_name'];//项目名称
        $article_name = $post['article_name'];//文章名称
        $author = $post['author'];//作者
        $periodical_name = $post['periodical_name'];//期刊名称
        $imageFile = $_FILES['imageFile'];
        $pilot_date = $post['pilot_date'];
//        var_dump($imageFile);die;
        //获取论文名称
        $other = new \app\models\SpecialOther;
        $thesisType = $other->thesisType($thesis_id);
        /**
         * 文件上传
         * 我们这里上传成功后把图片的地址进行返回
         */
        $redis = Yii::$app->cache;
        $accessToken = $redis->get('admin_'.$uid);
        //用户名
        $fnum = $accessToken['phone'];
        //当日日期
        $date = date('Y-m-d',time());
        //文件存储路径
        $file_dir = "uploads/ftp/" .$fnum ."/thesis/" .$thesisType['content']. "/" . $date;
        if (!is_dir($file_dir)){
            $res=mkdir(iconv("UTF-8", "GBK", $file_dir),0755,true);
        }

        $imgname = $imageFile["name"]; //获取上传的文件名称
        $filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀
        $newname = md5(time().$imgname).".".$filetype; //构建新名称
//文件不为空
        $researchOther = new \app\models\Thesis;
        $create_time = date('Y-m-d H:i:s', time());
        if(!empty($filetype)) {
            $file_dir = $file_dir . "/" . $newname;
            //数据库存储路径
            $realpath = "admin/uploads/ftp/" . $fnum . "/thesis/" . $thesisType['content'] . "/" . $date. "/" .$newname;
            //保存图片
            move_uploaded_file($imageFile['tmp_name'], $file_dir);

            //存附件详情表

            $addFileDetail = $researchOther->addFileDetail($imgname, 'd_thesis', $id, $filetype, $create_time, $uid);
            if (!$addFileDetail) {
                return tools::ajax_return_error('2', '保存附件失败', [2]);
            }
        }else{
            $realpath = '';
        }


        //添加论文表
        $addThesis = $researchOther->saveThesis($id, $project_id, $project_name, $thesis_id, $article_name, $periodical_name, $create_time, $author, $uid, 1, $pilot_date,$realpath);
        if (!$addThesis) {
            return tools::ajax_return_error('2', '保存附件失败', [1]);
        }




        return tools::ajax_return('0','保存成功',[]);



    }

    /**
     * title 提交论文
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionSubmitportthesis()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $thesis_id = $post['thesis_id'];//论文id
        $project_id = $post['project_id'];//项目id
        $project_name = $post['project_name'];//项目名称
        $article_name = $post['article_name'];//文章名称
        $author = $post['author'];//作者
        $periodical_name = $post['periodical_name'];//期刊名称
        $imageFile = $_FILES['imageFile'];
        $pilot_date = $post['pilot_date'];

        //校验
//        if(empty($thesis_id)){
//            return tools::ajax_return_error('2','请选择论文类型',[]);
//        }
//        if(empty($project_id)){
//            return tools::ajax_return_error('2','请选择论文项目',[]);
//        }
//        if(empty($article_name)){
//            return tools::ajax_return_error('2','请输入文章名称',[]);
//        }
//        if(empty($author)){
//            return tools::ajax_return_error('2','请输入作者',[]);
//        }
//        if(empty($periodical_name)){
//            return tools::ajax_return_error('2','请输入期刊名称',[]);
//        }
//        if(empty($imageFile)){
//            return tools::ajax_return_error('2','请上传PDF',[]);
//        }





        //获取论文名称
        $other = new \app\models\SpecialOther;
        $thesisType = $other->thesisType($thesis_id);

        /**
         * 文件上传
         * 我们这里上传成功后把图片的地址进行返回
         */
        $redis = Yii::$app->cache;
        $accessToken = $redis->get('admin_'.$uid);
        //用户名
        $fnum = $accessToken['phone'];
        //当日日期
        $date = date('Y-m-d',time());
        //文件存储路径
        $file_dir = "uploads/ftp/" .$fnum ."/thesis/" .$thesisType['content']. "/" . $date;
        if (!is_dir($file_dir)){
            $res=mkdir(iconv("UTF-8", "GBK", $file_dir),0755,true);
        }

        $imgname = $imageFile["name"]; //获取上传的文件名称
        $filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀
        $newname = md5(time().$imgname).".".$filetype; //构建新名称
        if(empty($filetype)) {
            return tools::ajax_return_error('2','请传文件',[]);
        }
        //文件不为空
        $researchOther = new \app\models\Thesis;
        $create_time = date('Y-m-d H:i:s', time());
        if(!empty($filetype)) {
            $file_dir = $file_dir . "/" . $newname;
            //数据库存储路径
            $realpath = "admin/uploads/ftp/" . $fnum . "/thesis/" . $thesisType['content'] . "/" . $date. "/" .$newname;
            //保存图片
            move_uploaded_file($imageFile['tmp_name'], $file_dir);


            //存附件详情表
            $addFileDetail = $researchOther->addFileDetail($imgname, 'd_thesis', $id, $filetype, $create_time, $uid);
            if (!$addFileDetail) {
                return tools::ajax_return_error('2', '提交附件失败', [1]);
            }

        }else{
            $realpath = '';
        }


        //添加论文表
        $addThesis = $researchOther->saveThesis($id, $project_id, $project_name, $thesis_id, $article_name, $periodical_name, $create_time,$author, $uid, 2, $pilot_date,$realpath);
        if (!$addThesis) {
            return tools::ajax_return_error('2', '提交附件失败', [1]);
        }


        return tools::ajax_return('0','提交成功',[]);
    }


    /**
     * title 论文详情
     * @return string
     */
    public function actionDetailportthesis()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];

        $thesis = new \app\models\Thesis;
        $detail = $thesis->detailThesis($id);
        if(!$detail){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$detail);


    }


    /**
     * title 删除论文
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionDelthesis()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];
        $thesis = new \app\models\Thesis;
        $del = $thesis->delThesis($id);
        if(!$del){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$del);
    }

    /**
     * title 条件搜索
     * @return string
     */
    public function actionSelectconditionthesis()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $type = $post['type'];
        $status = $post['status'];

        $thesis = new \app\models\Thesis;
        $result = $thesis->searchThesis($uid,$type,$status);

        if(!$result){
            return tools::ajax_return_error('0','成功',[]);
        }

        return tools::ajax_return('0','成功',$result);


    }




    /**
     * title 专利列表
     * @return string
     */
    public function actionPatentlistport()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $research = new \app\models\Patent;
        $list = $research->PatentList($uid);

        return tools::ajax_return('0','成功',$list);



    }


    /**
     * title 添加专利
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionAddportpatent()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        //添加论文
        $create_time = date('Y-m-d H:i:s',time());
        $other = new \app\models\Patent;
        $result = $other->addPatent($create_time,$uid);

        if($result == 1){
            return tools::ajax_return_error('1','添加失败',[]);
        }


        return tools::ajax_return('0','添加成功',$result);



    }


    /**
     * title 保存专利
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionSaveportpatent()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $patent_id = $post['patent_id'];//专利id
        $project_id = $post['project_id'];//项目id
        $project_name = $post['project_name'];//项目名称
        $article_name = $post['article_name'];//文章名称
        $author = $post['author'];//作者
//        $periodical_name = $post['periodical_name'];//期刊名称
        $imageFile = $_FILES['imageFile'];
        $pilot_date = $post['pilot_date'];

        //获取专利名称
        $other = new \app\models\SpecialOther;
        $patentType = $other->patentType($patent_id);

        /**
         * 文件上传
         * 我们这里上传成功后把图片的地址进行返回
         */
        $redis = Yii::$app->cache;
        $accessToken = $redis->get('admin_'.$uid);
        //用户名
        $fnum = $accessToken['phone'];
        //当日日期
        $date = date('Y-m-d',time());
        //文件存储路径
        $file_dir = "uploads/ftp/" .$fnum ."/patent/" .$patentType['id']. "/" . $date;
        if (!is_dir($file_dir)){
            $res=mkdir(iconv("UTF-8", "GBK", $file_dir),0755,true);
        }

        $imgname = $imageFile["name"]; //获取上传的文件名称
        $filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀
        $newname = md5(time().$imgname).".".$filetype; //构建新名称
//文件不为空
        $create_time = date('Y-m-d H:i:s', time());
        $patent = new \app\models\Patent;
        if(!empty($filetype)) {
            $file_dir = $file_dir . "/" . $newname;
            //数据库存储路径
            $realpath = "admin/uploads/ftp/" . $fnum . "/patent/" . $patentType['id'] . "/" . $date. "/" .$newname;
            //保存图片
            move_uploaded_file($imageFile['tmp_name'], $file_dir);

            //存附件详情表
            $thesis = new \app\models\Thesis;
            $addFileDetail = $thesis->addFileDetail($imgname, 'd_patent', $id, $filetype, $create_time, $uid);
            if (!$addFileDetail) {
                return tools::ajax_return_error('2', '保存附件失败', [2]);
            }

        }else{
            $realpath = '';
        }

            //添加论文表
            $addThesis = $patent->savePatent($id, $project_id, $project_name, $patent_id, $article_name,  $create_time, $realpath, $author, $uid, 1, $pilot_date);
            if (!$addThesis) {
                return tools::ajax_return_error('2', '保存附件失败', [1]);
            }


        return tools::ajax_return('0','保存成功',[]);



    }

    /**
     * title 提交专利
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionSubmitportpatent()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $patent_id = $post['patent_id'];//专利id
        $project_id = $post['project_id'];//项目id
        $project_name = $post['project_name'];//项目名称
        $article_name = $post['article_name'];//文章名称
        $author = $post['author'];//作者
//        $periodical_name = $post['periodical_name'];//期刊名称
        $imageFile = $_FILES['imageFile'];
        $pilot_date = $post['pilot_date'];

        //校验
        if(empty($patent_id)){
            return tools::ajax_return_error('2','请选择论文类型',[]);
        }
        if(empty($project_id)){
            return tools::ajax_return_error('2','请选择论文项目',[]);
        }
        if(empty($article_name)){
            return tools::ajax_return_error('2','请输入文章名称',[]);
        }
        if(empty($author)){
            return tools::ajax_return_error('2','请输入作者',[]);
        }
        if(empty($imageFile)){
            return tools::ajax_return_error('2','请上传PDF',[]);
        }





        //获取论文名称
        $other = new \app\models\SpecialOther;
        $patentType = $other->patentType($patent_id);

        /**
         * 文件上传
         * 我们这里上传成功后把图片的地址进行返回
         */
        $redis = Yii::$app->cache;
        $accessToken = $redis->get('admin_'.$uid);
        //用户名
        $fnum = $accessToken['phone'];
        //当日日期
        $date = date('Y-m-d',time());
        //文件存储路径
        $file_dir = "uploads/ftp/" .$fnum ."/patent/" .$patentType['id']. "/" . $date;
        if (!is_dir($file_dir)){
            $res=mkdir(iconv("UTF-8", "GBK", $file_dir),0755,true);
        }

        $imgname = $imageFile["name"]; //获取上传的文件名称
        $filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀
        $newname = md5(time().$imgname).".".$filetype; //构建新名称

        if(empty($filetype)) {
            return tools::ajax_return_error('2','请传文件',[]);
        }
        //文件不为空
        $create_time = date('Y-m-d H:i:s', time());
        $patent = new \app\models\Patent;
        if(!empty($filetype)) {
            $file_dir = $file_dir . "/" . $newname;
            //数据库存储路径
            $realpath = "admin/uploads/ftp/" . $fnum . "/patent/" . $patentType['id'] . "/" . $date. "/" .$newname;
            //保存图片
            move_uploaded_file($imageFile['tmp_name'], $file_dir);

            //存附件详情表
            $thesis = new \app\models\Thesis;
            $addFileDetail = $thesis->addFileDetail($imgname, 'd_patent', $id, $filetype, $create_time, $uid);
            if (!$addFileDetail) {
                return tools::ajax_return_error('2', '保存附件失败', [2]);
            }

        }else{
            $realpath = '';
        }


        //添加论文表
        $addThesis = $patent->savePatent($id, $project_id, $project_name, $patent_id, $article_name, $create_time, $realpath, $author, $uid, 2, $pilot_date);
        if (!$addThesis) {
            return tools::ajax_return_error('2', '保存附件失败', [1]);
        }


        return tools::ajax_return('0','提交成功',[]);


    }


    /**
     * title 专利详情
     * @return string
     */
    public function actionDetailportpatent()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];

        $patent = new \app\models\Patent;
        $detail = $patent->detailPatent($id);
        if(!$detail){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$detail);


    }


    /**
     * title 删除论文
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionDelpatent()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];
        $patent = new \app\models\Patent;
        $del = $patent->delPatent($id);
        if(!$del){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$del);
    }

    /**
     * title 专利条件搜索
     * @return string
     */
    public function actionSelectconditionpatent()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $type = $post['type'];
        $status = $post['status'];

        $patent = new \app\models\Patent;
        $result = $patent->searchThesis($uid,$type,$status);

        if(!$result){
            return tools::ajax_return_error('0','成功',[]);
        }

        return tools::ajax_return('0','成功',$result);


    }



    /**
     * title 添加示范应用
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionAddportpilot()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        //添加论文
        $create_time = date('Y-m-d H:i:s',time());
        $other = new \app\models\Pilot;
        $result = $other->addPilot($create_time,$uid);

        if($result == 1){
            return tools::ajax_return_error('1','添加失败',[]);
        }


        return tools::ajax_return('0','添加成功',$result);



    }



    /**
     * title 示范应用条件搜索  列表
     * @return string
     */
    public function actionSelectsearchpilot()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $status = $post['status'];

        $patent = new \app\models\Pilot;
        $result = $patent->searchPilot($uid,$status);

        if(!$result){
            return tools::ajax_return_error('0','成功',[]);
        }

        return tools::ajax_return('0','成功',$result);


    }


    /**
     * title 保存示范应用
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionSaveportpilot()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $project_id = $post['project_id'];//项目id
        $project_name = $post['project_name'];//项目名称
        $pilot_name = $post['pilot_name'];//示范应用名称
        $location = $post['place'];//地点
        $description = $post['description'];//描述
        $imageFile = $_FILES['imageFile'];
        $pilot_date = $post['pilot_date'];//试点时间


        /**
         * 文件上传
         * 我们这里上传成功后把图片的地址进行返回
         */
        $redis = Yii::$app->cache;
        $accessToken = $redis->get('admin_'.$uid);
        //用户名
        $fnum = $accessToken['phone'];
        //当日日期
        $date = date('Y-m-d',time());
        //文件存储路径
        $file_dir = "uploads/img/" .$fnum ."/pilot/" . $date;
        if (!is_dir($file_dir)){
            $res=mkdir(iconv("UTF-8", "GBK", $file_dir),0755,true);
        }

        $imgname = $imageFile["name"]; //获取上传的文件名称
        $filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀
        $newname = md5(time().$imgname).".".$filetype; //构建新名称

        //文件不为空
        $pilot = new \app\models\Pilot;
        $create_time = date('Y-m-d H:i:s', time());

        if(!empty($filetype)) {
            $file_dir = $file_dir . "/" . $newname;
            //数据库存储路径
            $realpath = "admin/uploads/img/" . $fnum . "/pilot/" . $date. "/" .$newname;
            //保存图片
            move_uploaded_file($imageFile['tmp_name'], $file_dir);

            //存附件详情表
            $thesis = new \app\models\Thesis;
            $addFileDetail = $thesis->addFileDetail($imgname, 'd_pilot', $id, $filetype, $create_time, $uid);
            if (!$addFileDetail) {
                return tools::ajax_return_error('2', '保存附件失败', [2]);
            }

        }else{
            $realpath = '';
        }


        //保存示范应用表
        $addThesis = $pilot->savePilot($id, $project_id, $project_name, $pilot_name, $location, $description, $create_time, $realpath, $uid, 1, $pilot_date);
        if (!$addThesis) {
            return tools::ajax_return_error('2', '保存附件失败', [1]);
        }



        return tools::ajax_return('0','保存成功',[]);



    }

    /**
     * title 提交示范应用
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionSubmitportpilot()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $project_id = $post['project_id'];//项目id
        $project_name = $post['project_name'];//项目名称
        $pilot_name = $post['pilot_name'];//示范应用名称
        $location = $post['place'];//地点
        $description = $post['description'];//描述
        $imageFile = $_FILES['imageFile'];
        $pilot_date = $post['pilot_date'];//试点时间
        //校验
        if(empty($project_id)){
            return tools::ajax_return_error('2','请选择项目',[]);
        }
        if(empty($pilot_name)){
            return tools::ajax_return_error('2','请输入示范应用名称',[]);
        }
        if(empty($location)){
            return tools::ajax_return_error('2','请输入地点',[]);
        }
        if(empty($description)){
            return tools::ajax_return_error('2','请输入描述',[]);
        }
        if(empty($pilot_date)){
            return tools::ajax_return_error('2','请输入试点时间',[]);
        }





        /**
         * 文件上传
         * 我们这里上传成功后把图片的地址进行返回
         */
        $redis = Yii::$app->cache;
        $accessToken = $redis->get('admin_'.$uid);
        //用户名
        $fnum = $accessToken['phone'];
        //当日日期
        $date = date('Y-m-d',time());
        //文件存储路径
        $file_dir = "uploads/img/" .$fnum ."/pilot/" . $date;
        if (!is_dir($file_dir)){
            $res=mkdir(iconv("UTF-8", "GBK", $file_dir),0755,true);
        }

            $imgname = $imageFile["name"]; //获取上传的文件名称
            $filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀
            $newname = md5(time() . $imgname) . "." . $filetype; //构建新名称

        if(empty($filetype)) {
            return tools::ajax_return_error('2','请传文件',[]);
        }
        //文件不为空
        $pilot = new \app\models\Pilot;
        $create_time = date('Y-m-d H:i:s', time());
        if(!empty($filetype)) {
            $file_dir = $file_dir . "/" . $newname;
            //数据库存储路径
            $realpath = "admin/uploads/img/" . $fnum . "/pilot/" . $date. "/" .$newname;
            //保存图片
            move_uploaded_file($imageFile['tmp_name'], $file_dir);

            //存附件详情表
            $thesis = new \app\models\Thesis;
            $addFileDetail = $thesis->addFileDetail($imgname, 'd_pilot', $id, $filetype, $create_time, $uid);
            if (!$addFileDetail) {
                return tools::ajax_return_error('2', '保存附件失败', [2]);
            }

        }else{
            $realpath = '';
        }

            //保存示范应用表
            $addThesis = $pilot->savePilot($id, $project_id, $project_name, $pilot_name, $location, $description, $create_time, $realpath, $uid, 2, $pilot_date);
            if (!$addThesis) {
                return tools::ajax_return_error('2', '保存附件失败', [1]);
            }


        return tools::ajax_return('0','提交成功',[]);


    }




    /**
     * title 示范应用详情
     * @return string
     */
    public function actionDetailportpilot()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];

        $pilot = new \app\models\Pilot;
        $detail = $pilot->detailPilot($id);
        if(!$detail){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$detail);


    }


    /**
     * title 删除示范应用
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionDelpilot()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];
        $pilot = new \app\models\Pilot;
        $del = $pilot->delPilot($id);
        if(!$del){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$del);
    }



    /**
     * 接口
     * title 添加专家管理
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionAddPortSpecial()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $other = new\app\models\SpecialManage;
        $create_time = date('Y-m-d H:i:s',time());
        $addSpecial = $other->addSpecial($uid,$create_time);
        if($addSpecial == 1){
            return tools::ajax_return_error('1','添加失败',[]);
        }

        return tools::ajax_return('0','添加成功',$addSpecial);

    }


    /**
     * 接口
     * title 保存专家管理
     * @return string
     */
    public function actionSavePortSpecial()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $child_id = $post['child_id'];//子表id
        $special_name = $post['special_name'];//项目名称
        $special = $post['special'];
        $specials = json_decode($special,true);
//        $specials = [['phone'=>1,'company'=>1,'name'=>1,'job'=>1,'plan_id'=>1],['phone'=>2,'company'=>2,'name'=>2,'job'=>2,'plan_id'=>2]];

//        $plan_id = $post['plan_id'];//进度id
//        $name = $post['name'];//专家姓名
//        $phone = $post['phone'];//专家电话
//        $company = $post['company'];//专家所属单位
//        $job = $post['job'];//职务

        $update_time = date('Y-m-d H:i:s',time());
        $special = new \app\models\SpecialManage;
        $save = $special->saveSpecial($id,$child_id,$special_name,1,$update_time,$specials);
        if(!$save){
            return tools::ajax_return_error('1','保存失败',[]);
        }
        return tools::ajax_return('0','保存成功',$save);


    }


    /**
     * 接口
     * title 提交专家管理
     * @return string
     */
    public function actionSubmitPortSpecial()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];//列表id
        $child_id = $post['child_id'];//子表id
        $special_name = $post['special_name'];//项目名称
        $check_think = $post['check_think'];//验收意见

        if(empty($id)){
            return tools::ajax_return('2','请传id',[]);
        }
        if(empty($child_id)){
            return tools::ajax_return('2','请输入child_id',[]);
        }
        if(empty($special_name)){
            return tools::ajax_return('2','请输入项目名称',[]);
        }
        if(empty($check_think)){
            return tools::ajax_return('2','请输入验收意见',[]);
        }

        $special = $post['special'];

        if(empty($special)){
            return tools::ajax_return('2','请输入相关信息',[]);
        }

        $specials = json_decode($special,true);

        if(empty($specials['plan_id'])){
            return tools::ajax_return('2','请输入相关信息',[]);
        }
        if(empty($specials['name'])){
            return tools::ajax_return('2','请输入相关信息',[]);
        }
        if(empty($specials['phone'])){
            return tools::ajax_return('2','请输入相关信息',[]);
        }
        if(empty($specials['company'])){
            return tools::ajax_return('2','请输入相关信息',[]);
        }
        if(empty($specials['job'])){
            return tools::ajax_return('2','请输入相关信息',[]);
        }


//        $specials = [['phone'=>1,'company'=>1,'name'=>1,'job'=>1,'plan_id'=>1],['phone'=>2,'company'=>2,'name'=>2,'job'=>2,'plan_id'=>2]];


        $update_time = date('Y-m-d H:i:s',time());
        $special = new \app\models\SpecialManage;
        $save = $special->saveSpecial($id,$child_id,$special_name,2,$update_time,$specials);
        if(!$save){
            return tools::ajax_return_error('1','提交失败',[]);
        }
        return tools::ajax_return('0','提交成功',$save);


    }


    /**
     * 接口
     * title 专家管理列表
     * @return string
     */
    public function actionSpecialPortList()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $special = new \app\models\SpecialManage;
        $list = $special->specialList($uid);
        if(!$list){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$list);

    }


    /**
     * 接口
     * title 删除专家管理列表
     * @return string
     */
    public function actionDelSpecial()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }
        $id = $post['id'];

        $special = new \app\models\SpecialManage;
        $del = $special->delSpecial($id);

        if($del == 2){
            return tools::ajax_return_error('1','删除失败',[]);
        }

        return tools::ajax_return('0','删除成功',$del);

    }



    /**
     * 接口
     * title 专家管理详情
     * @return string
     */
    public function actionDetailPortSpecial()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $id = $post['id'];

        $special = new \app\models\SpecialManage;
        $detail = $special->detailSpecial($id);

        if(!$detail){
            return tools::ajax_return_error('1','失败',[]);
        }

        return tools::ajax_return('0','成功',$detail);
    }






}